package com.zhou.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.blog.dao.pojo.SysUser;
import com.zhou.blog.dao.pojo.SysUser;
import com.zhou.blog.service.LoginService;
import com.zhou.blog.service.SysUserService;
import com.zhou.blog.dao.mapper.SysUserMapper;
import com.zhou.blog.utils.JWTUtils;
import com.zhou.blog.vo.ErrorCode;
import com.zhou.blog.vo.LoginUserVo;
import com.zhou.blog.vo.Result;
import com.zhou.blog.vo.UserVo;
import com.zhou.blog.vo.params.LoginParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 学习人
* @description 针对表【ms_sys_user】的数据库操作Service实现
* @createDate 2022-04-17 14:21:17
*/
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    LoginService loginService;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public SysUser findUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setNickname("默认用户");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);


    }

    /**
     * 根据token查询用户信息
     * @param token
     * @return LoginUserVo
     */
    @Override
    public Result findUserByToken(String token) {
        if (StringUtils.isBlank(token)) {
            return Result.fail(ErrorCode.TOKEN_ERR.getCode(), ErrorCode.TOKEN_ERR.getMsg());
        }
        //校验redis中是否存在
        SysUser sysUser = loginService.checkToken(token);
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setId(String.valueOf(sysUser.getId()));

        return Result.success(loginUserVo);
    }

    @Override
    public int addUser(LoginParams loginParams) {
        return 0;
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo findUserVoById(Long authorId) {
        UserVo userVo = new UserVo();
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("默认用户");
        }
        BeanUtils.copyProperties(sysUser,userVo);
        userVo.setId(String.valueOf(sysUser.getId()));
        return userVo;
    }
}




